package day9.guanlixitong.src.ui;
import javax.swing.*;
import javax.swing.table.DefaultTableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import day9.guanlixitong.src.bean.Employee;

public class EmployeeManager extends JFrame {

    private JTable table;
    private DefaultTableModel model;
    private JTextField searchField;
    private JButton searchButton, addButton;
 private EmployeeManager E;
   static List<Employee> employees;

//    public static void main(String[] args) {
//
//            new EmployeeManager();
//
//
//    }
    public EmployeeManager(String username) {
   this.E=this;

        setTitle("欢迎"+username+"进入管理系统");
        setSize(800, 600);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);

        // 初始化员工列表
        employees = new ArrayList<>();
//        for (int i = 1; i <= 20; i++) {
//            employees.add(new Employee(i, "姓名" + i, "部门" + (i % 5), "职位" + (i % 3),+ 30000));
//        }

        // 创建模型并设置到表格中
        String[] columnNames = {"ID", "姓名", "部门", "职位","薪水"};
        model = new DefaultTableModel(columnNames, 0) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false; // 不允许直接编辑表格内容
            }
        };
        for (Employee e : employees) {
            model.addRow(new Object[]{e.getId(), e.getName(), e.getDepartment(), e.getPosition(),e.getMoney()});
        }
        table = new JTable(model);
        table.setFillsViewportHeight(true);

        // 添加右键菜单
        JPopupMenu popupMenu = new JPopupMenu();
        JMenuItem editItem = new JMenuItem("修改");
        JMenuItem deleteItem = new JMenuItem("删除");
        popupMenu.add(editItem);
        popupMenu.add(deleteItem);

        // 设置右键菜单的监听器
        table.addMouseListener(new MouseAdapter() {
            @Override
            public void mousePressed(MouseEvent e) {
                if (SwingUtilities.isRightMouseButton(e)) {
                    int row = table.rowAtPoint(e.getPoint());
                    if (row >= 0 && row < table.getRowCount()) {
                        table.setRowSelectionInterval(row, row);
                        popupMenu.show(e.getComponent(), e.getX(), e.getY());
                    }
                }
            }
        });

        // 设置布局
        JPanel panel = new JPanel(new BorderLayout());

        // 输入框、按钮区域
        JPanel topPanel = new JPanel();
        topPanel.setLayout(new FlowLayout(FlowLayout.CENTER));
        searchField = new JTextField(20);
        searchButton = new JButton("搜索");
        addButton = new JButton("添加");
        topPanel.add(searchField);
        topPanel.add(searchButton);
        topPanel.add(addButton);

        // 表格区域
        JScrollPane scrollPane = new JScrollPane(table);

        // 组合面板
        panel.add(topPanel, BorderLayout.NORTH);
        panel.add(scrollPane, BorderLayout.CENTER);

        add(panel);

        // 按钮事件处理
        addButton.addActionListener(this::addEmployee);
        editItem.addActionListener(this::modifyEmployee);
        deleteItem.addActionListener(this::deleteEmployee);
       searchButton .addActionListener(new ActionListener() {
           @Override
           public void actionPerformed(ActionEvent e)
           {
               if(!searchField.getText().equals(""))SearchId(Integer.parseInt(searchField.getText()));
//                else{
//                   // 如果搜索框为空，显示提示对话框
//                   JOptionPane.showMessageDialog(
//                           SwingUtilities.windowForComponent(searchButton), // 使用当前窗口实例
//                           "搜索框不能为空！",
//                           "错误",
//                           JOptionPane.ERROR_MESSAGE
//                   );
//               }
               else {JOptionPane.showMessageDialog(EmployeeManager.this.E,"搜索框不能为空");}//在匿名内部类中使用外部类的成员变量的方法
           }
       });
        this.setVisible(true);
    }

    private void addEmployee(ActionEvent e) {
        // 在这里实现添加员工逻辑
//        System.out.println("添加员工功能待实现...");
        new AddEmployeeFrame(this);
    }
      public void SearchId(int id)
      {      boolean flag=false;
          for(int i=0;i<employees.size();i++)
          {
                     if(employees.get(i).getId()==id)
                     {
                         JOptionPane.showMessageDialog(this,"Id:"+id+"\n Name:"+employees.get(i).getName()+"\nDepartment:"+employees.get(i).getDepartment()+"\nPosition:"+employees.get(i).getPosition()+"\nMoneny:"+employees.get(i).getMoney());
                         flag=true;
                     }
          }
        if(flag==false)JOptionPane.showMessageDialog(this,"没有这个id");
      }

    private void modifyEmployee(ActionEvent e) {
        int selectedRow = table.getSelectedRow();
        if (selectedRow != -1) {
            int id = (int) model.getValueAt(selectedRow, 0); // 获取选中行的ID
            System.out.println("修改员工ID: " + id);
            // 在这里可以弹出一个对话框来修改员工信息

            JOptionPane.showMessageDialog(this, "修改员工ID: " + id + "的信息");
        } else {
            JOptionPane.showMessageDialog(this, "请选择要修改的员工");
        }
    }

    private void deleteEmployee(ActionEvent e) {
        int selectedRow = table.getSelectedRow();

        if (selectedRow != -1) {
            int id = (int) model.getValueAt(selectedRow, 0); // 获取选中行的ID，第一行，第0列
            // System.out.println("删除员工ID: " + id);
            JOptionPane.showMessageDialog(this, "删除员工ID: " + id + "的信息");
            model.removeRow(selectedRow); // 从表格中移除该行
         for(int i=0;i<employees.size();i++)
         {
             if(employees.get(i).getId()==id)
             {
                 employees.remove(i);
             }
         }
         JOptionPane.showMessageDialog(this,"删除成功");
        }
        else {
            JOptionPane.showMessageDialog(this, "请选择要删除的员工");
        }

    }


    public void addEmployeer(Employee Employees) {
        employees.add(Employees);
        model.addRow(new Object[]{Employees.getId(),Employees.getName(),Employees.getDepartment(),Employees.getPosition(),Employees.getMoney()});

    }
}